#include "StdAfx.h"
#include <CommonLib/DatabaseUtils.h>

const std::string& QuotesIfEmpty(const std::string& str)
{
	static std::string strQuotes("\"\"");
	if (str.empty())
		return strQuotes;

	return str;
}

void RemoveQuotes(fastbird::StringVector& strings)
{
	int count = strings.size();
	for (int i=0; i<count; i++)
	{
		std::string& string = strings[i];
		int len = string.length();
		if (len < 2)
		{
			if (string==".")
				string.clear();
			continue;
		}
		if (string.at(0) == '"' && string.at(len-1) == '"')
		{
			string.erase(string.end()-1);
			string.erase(string.begin());
		}
		if (string==".")
			string.clear();
	}
}

void ExpandDoubleComma(char line[], int buffersize)
{
	int size = strlen(line);
	if (size==0)
		return;
	for (int i=0; i<size; i++)
	{
		if (line[i]==0)
			return;
		if (i==0)
			continue;
		if (line[i-1] == ',' && line[i]==',')
		{
			assert(size+1 < buffersize);
			line[size+1] = 0;
			for (int dest=size; dest>i; dest--)
			{
				line[dest] = line[dest-1];
			}
			line[i] = ' ';
			size = strlen(line);
			i++;
		}
	}
}

DWORD ParseVersion(char line[])
{
	int size = strlen(line);
	if (size<2)
		return 0;
	if (line[0] == '#' && line[1]=='!')
	{
		std::string str(&line[3]);
		DWORD version = fastbird::StringConverter::parseLong(str);
		return version;
	}
	else
	{
		return 0;
	}
}